package com.leo.offer;

import java.util.LinkedList;

/**
 * 用两个栈实现一个队列
 * @author xuexiaolei
 * @version 2018年03月01日
 */
public class T07StackQueue {
    LinkedList sTail = new LinkedList();
    LinkedList sHead = new LinkedList();
    public void offer(int val){
        while (!sHead.isEmpty()){ sTail.push(sHead.pop());}
        sTail.push(val);
    }

    public int poll(){
        while (!sTail.isEmpty()){ sHead.push(sTail.pop());}
        if (sHead.isEmpty()){ return -1; }
        return (int) sHead.poll();
    }

    public static void main(String[] args) {
        T07StackQueue queue = new T07StackQueue();
        queue.offer(1);
        System.out.println(queue.poll());
        queue.offer(2);
        queue.offer(3);
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        queue.offer(4);
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }
}
